package com.windgo.controller.admin;

import com.github.pagehelper.PageInfo;
import com.windgo.bean.MarketAdminUpdateAdminVo;
import com.windgo.bean.MarketAdmin;
import com.windgo.bean.MarketAdminAdminCreateVo;
import com.windgo.bean.MarketAdminAdminListVo;
import com.windgo.bean.common.BasePageInfo;
import com.windgo.bean.common.BasePageInfo1;
import com.windgo.bean.common.BaseRespVo;
import com.windgo.bean.common.CommonData;
import com.windgo.service.AdminService;
import com.windgo.util.StringUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 张林浩
 * @since 2022/10/12 14:42
 */
@RestController
@RequestMapping("admin/admin")
public class AdminAdminController {
    @Autowired
    AdminService adminService;

    @RequestMapping("list")
    public BaseRespVo adminAdminList(BasePageInfo1 info) {
        CommonData<MarketAdminAdminListVo> data = null;
        if (StringUtil.isEmpty(info.getUsername())){
            BasePageInfo pageInfo = new BasePageInfo();
            pageInfo.setLimit(info.getLimit());
            pageInfo.setPage(info.getPage());
            pageInfo.setOrder(info.getOrder());
            pageInfo.setSort(info.getSort());
             data = adminService.selectAdminList(pageInfo);
        }else {
             data = adminService.selectAdminList1(info);
        }
        return BaseRespVo.ok(data);
    }

    @PostMapping("create")
    public BaseRespVo adminCreateAdmin(@RequestBody MarketAdminAdminCreateVo adminListVo) {
        MarketAdminAdminCreateVo marketAdminAdminCreateVo = adminService.insertAdmin(adminListVo);
        return BaseRespVo.ok(marketAdminAdminCreateVo);
    }

    @PostMapping("update")
    public BaseRespVo adminUpdateAdmin(@RequestBody MarketAdminAdminCreateVo adminCreateVo){
        MarketAdminUpdateAdminVo marketAdminUpdateAdminVo = adminService.updateAdmin(adminCreateVo);
        return BaseRespVo.ok(marketAdminUpdateAdminVo);
    }

   @PostMapping("delete")
    public BaseRespVo adminDeleteAdmin(@RequestBody MarketAdmin marketAdmin){

       Subject subject = SecurityUtils.getSubject();
       MarketAdmin primaryPrincipal = (MarketAdmin) subject.getPrincipals().getPrimaryPrincipal();
       int code = adminService.deleteAdmin(marketAdmin,primaryPrincipal);
        if (code == 200){
            return BaseRespVo.success();
        }else if (code == 300){
            return BaseRespVo.ok(300,"您没有该权限");
        }
        return BaseRespVo.failed1();

   }
}